package model
{
	import view.GameView;

	public class Game
	{
		public const RED:int = 1;
		public const GREEN:int = 2;
		public const BLUE:int = 3;
		public const YELLOW:int = 4;
		
		public const SQUARE_BRICK:int = 100;
		public const ROUND_BRICK:int = 200;
		
		private var squareBrick:SquareBrick = new SquareBrick();
		private var roundBrick:RoundBrick = new RoundBrick();
		
		public var startArray:Array = new Array();
		public var brickArray:Array = new Array();
		
		private var gameplanSide:int;
		public var currentBrickType:int = 100;
		
		public var targetColor:int = 0;
		
		
		
		public function Game():void
		{
			setPlanSide(8);
			CreateBricks(new SquareBrick);
			setTargetColor();
		}
		
		//skapar brickor som blandas om och sparas i brick arrayen
		public function CreateBricks(aBrick:IBrick):void
		{
			var bricksTotal:int = gameplanSide * gameplanSide;
			var howManyOfEachColor:int = bricksTotal/4;
			var brickColor:int = RED;//dvs 1
			var colorCount:int = 0;
			
			aBrick.setColor(brickColor);
			
			for(var i:int = 0; i < bricksTotal; i++)
			{
				var brick:IBrick;
				
				if(aBrick.getType() == squareBrick.getType())
				{
					brick = new SquareBrick();
				}
				else if(aBrick.getType() == roundBrick.getType())
				{
					brick = new RoundBrick();
				}
				
				brick.setColor(brickColor);
				
				if(colorCount < howManyOfEachColor)
				{
					colorCount++;
				}
				else
				{
					colorCount = 1;
					brickColor++;
					brick.setColor(brickColor);
				}
				startArray[i] = brick;
			}
			while (startArray.length > 0)
			{
				brickArray.push(startArray.splice(Math.round(Math.random() * (startArray.length - 1)), 1)[0]);
			}
		}
		
		public function setPlanSide(aSide:int):void
		{
			gameplanSide = aSide;
		}
		
		public function getPlanSide():int
		{
			return gameplanSide;
		}
		
		public function getBricks():Array
		{
			return brickArray;
		}
		
		//för att vet på vilken bricka man ska sikta
		public function setTargetColor():void
		{
			targetColor = Math.round(Math.random()*(4-1))+1;
		}
		
		public function getTargetColor():int
		{
			return targetColor;
		}
		
		public function RestartBricks():void
		{
			//brickArray = null;
			
			if(currentBrickType == SQUARE_BRICK)
			{
				CreateBricks(new SquareBrick);
				currentBrickType = SQUARE_BRICK;
			}
			else
			{
				CreateBricks(new RoundBrick);
				currentBrickType = ROUND_BRICK;
			}
		}
		
	}
}